<template>
  <view class="login-container">
    <uni-icons type="contact-filled" class="tx" color="#AFAFAF" size="100"></uni-icons>
    <button class="login-button" type="primary" @click="loginHandler">一键登录</button>
    <view class="tip">登录后尽享更多权益</view>
  </view>
</template>

<script>
  import {mapMutations} from 'vuex'
  export default {
    name:"Login",
    data() {
      return {
        
      };
    },
    methods: {
      ...mapMutations('userAbout', ['UPDATEUSERINFO', 'UPDATETOKEN']),
      // 登录获取用户信息
      loginHandler() {
        uni.getUserProfile({
          desc: '你的授权信息',
          success: (res) => {
            // 将信息存到 vuex 中
            this.UPDATEUSERINFO(res.userInfo)
            // 获取token
            this.getToken(res)
          },
          fail: (res) => {
            return uni.$showMsg('您取消了登录授权')
          }
      })
    },
    // 获取登录的token
    getToken(info) {
      const _this = this
      uni.login({
        async success (res) {
          if (res.code) {
            const query = {
              code: res.code,
              encryptedData: info.encryptedData,
              iv: info.iv,
              rawData: info.rawData,
              signature: info.signature
            }
            /*此处接口有问题，不能返回token，因此自己设置一个假的token
              Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyLCJpYXQiOjE1MjU0MDIyMjMsImV4cCI6MTUyNTQ4ODYyM30.g-4GtEQNPwT_Xs0Pq7Lrco_9DfHQQsBiOKZerkO-O-o
            */
            const token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyLCJpYXQiOjE1MjU0MDIyMjMsImV4cCI6MTUyNTQ4ODYyM30.g-4GtEQNPwT_Xs0Pq7Lrco_9DfHQQsBiOKZerkO-O-o"
            const result = await uni.$http.post('/api/public/v1/users/wxlogin', query)
            if (result.data.meta.status !== 400) return uni.$showMsg('登录失败')
            // uni.$showMsg('登录成功')
            // 保存token到vuex
            _this.UPDATETOKEN(token)
          }
        },
        fail() {
          uni.$showMsg('登录失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
  .login-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 750rpx;
    background-color: #f8f8f8;
    overflow: hidden;
    position: relative;
    // 绘制登录盒子底部的半椭圆造型
    &::after {
      content: ' ';
      display: block;
      position: absolute;
      width: 100%;
      height: 40px;
      left: 0;
      bottom: 0;
      background-color: white !important;
      border-radius: 100%;
      transform: translateY(50%);
    }
    .tx {
      margin-top: 50px;
    }
    .login-button {
      background-color: #C00000;
      width: 90%;
      border-radius: 100px;
      margin: 15px 0;
    }
    .tip {
      font-size: 12px;
      color: 'gray';
    }
  }
</style>